package hsl.study.v3;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author HSL
 * @version 1.0
 * @data 2025/11/17 11:25
 * @desc
 */

public class CglibProxyHandler implements MethodInterceptor {
    public Object target;

    public CglibProxyHandler(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("[CGLIB代理] 调用方法：" + method.getName());
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("[CGLIB代理] 方法执行完毕");
        return result;
    }
}
